﻿using BookManage.BLL;
using BookManage.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookManage
{
    public partial class frmManage : Form
    {
        private DataTable dt = null;//存放查询结果，并给DataGridView dgvReader提供数据
        private Reader reader = new Reader();
        private ReaderAdmin readerBLL = new ReaderAdmin();
        public frmManage()
        {
            InitializeComponent();
        }
        private void ShowData()
        {
            dgvReader.DataSource = dt;
            foreach (DataColumn dc in dt.Columns)
            {
                dgvReader.Columns[dc.ColumnName].HeaderText = Reader.ColumnTitle(dc.ColumnName);
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            int rdID;
            string rdName;
            if (txtrdID.Text.Trim() == "") 
            {
                rdID = 0;
            }
            else
            {
                int i = txtrdID.Text.IndexOf("--");
                if (i > 0)
                    rdID = Convert.ToInt32(txtrdID.Text.Substring(0, i));
                else
                    rdID = Convert.ToInt32(txtrdID.Text);
            }
            rdName = txtrdName.Text;
            /*
                先调用readerBLL类中的GetReader(),GetReader()中又调用了ReaderDAL.GetReader()
            */
            dt = readerBLL.GetReaderByRD(rdID,rdName);//ReaderDAL.GetReader(rdType, rdDept, rdName)的判断语句,无法判断单位和姓名
            ShowData();
        }

        private void btnAllQry_Click(object sender, EventArgs e)
        {
            dt = readerBLL.GetAllReader();
            ShowData();
        }

        private void RdBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (txtrdID.Text.Trim() == "" || txtAdminRoles.Text.Trim() == "")
                MessageBox.Show("用户号和用户权限不能为空！！！");
            else
            {
                /*rdID = Convert.ToInt32(txtrdID.Text);
                rdAdminRoles = Convert.ToInt32(txtAdminRoles.Text);
                dt = readerBLL.ChangeRoles(rdID, rdAdminRoles);//ReaderDAL.GetReader(rdType, rdDept, rdName)的判断语句,无法判断单位和姓名
                ShowData();*/
                reader.rdID = Convert.ToInt32(txtrdID.Text);
                reader.rdAdminRoles = Convert.ToInt32(txtAdminRoles.Text);
                UserAdmin.ChangeRoles(reader);
                MessageBox.Show("修改成功！！！该用户角色现为——" + reader.rdAdminRoles);
            }
        }
    }
}
